from fastapi import FastAPI,Request
from starlette.responses import HTMLResponse
from starlette.staticfiles import StaticFiles
from starlette.templating import Jinja2Templates
from tortoise.contrib.fastapi import register_tortoise
import uvicorn
from settings import TORTOISE_ORM
from api.student import student_api
from tortoise import Tortoise



app=FastAPI()

# 添加静态文件 添加背景图
app.mount("/images",StaticFiles(directory="images"),name="images")

templates = Jinja2Templates(directory="templates")
# 挂载学生路由
app.include_router(student_api,prefix="/student",tags=["选课系统学生接口"]) #prefix="student"表示后续修改路径时只需要改这个地方
register_tortoise(
    app=app,
    # 数据库连接和模型的详细配置
    config=TORTOISE_ORM,
)


@app.on_event("startup")
async def startup_event():
    # 自动创建所有模型对应的表（仅在表不存在时创建）
    await Tortoise.generate_schemas()


# 全局搜索

@app.get("/",response_class=HTMLResponse)
async def index(request:Request):
    return templates.TemplateResponse("index.html",{"request":request})



@app.get("/models")
async def get_models():
    return {"models":"student"}

if __name__ == '__main__':
    uvicorn.run("app:app", host="0.0.0.0", port=8005, reload=True)

